from selenium.webdriver.common.by import By
import time
import logging
import pytest
import allure
from Base.Base import Base


class Page(Base):
    name_loc = (By.NAME, 'email')
    pwd_loc = (By.NAME, 'password')
    peng_loc = (By.ID, 'dologin')
    frame_loc = (By.XPATH, '/html/body/div[3]/div[3]/div[1]/div/div[3]/div[1]/div[2]/iframe')

    def go_frame(self):
        with allure.step("进入 frame 框架"):
            self.switch_fream(0)
        logging.info("进入 frame 框架")

    def login_name(self, text):
        with allure.step(f"输入用户名: {text}"):
            self.send_keys(self.name_loc, text)
        logging.info(f"输入用户名: {text}")

    def login_pwd(self, pwd):
        with allure.step(f"输入密码: {pwd}"):
            self.send_keys(self.pwd_loc, pwd)
        logging.info(f"输入密码: {pwd}")

    def login_deng(self):
        with allure.step("点击登录按钮"):
            self.click(self.peng_loc)
        logging.info("点击登录按钮")

    def login_clear(self):
        with allure.step("清空用户名输入框"):
            self.clear(self.name_loc)
        logging.info("清空用户名输入框")

    def go_login(self, name, pwd):
        with allure.step("打开 163 邮箱登录页面"):
            self.open_url('https://mail.163.com/')
        logging.info("打开 163 邮箱登录页面")
        time.sleep(2)

        self.go_frame()
        time.sleep(2)

        self.login_clear()
        time.sleep(2)

        self.login_name(name)
        self.login_pwd(pwd)
        time.sleep(3)

        self.login_deng()
        time.sleep(5)

        with allure.step("退出 frame 框架"):
            self.switch_file()
        logging.info("退出 frame 框架")